package com.example.smartcity_poverty.ui.AllService.OA.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.OA.adapter.OaAddMeAdapter
import com.example.smartcity_poverty.ui.AllService.OA.model.OaShowMeEntity
import kotlinx.android.synthetic.main.activity_oa_show_me.*
import kotlinx.coroutines.Dispatchers

/**
 * 就诊人信息
 */
class OaShowMeActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_oa_show_me)
        title = "就诊人信息"

        coroutine(Dispatchers.IO){
            // 获取个人信息
            getPerson()
        }

        // 点击添加个人信息
        oaMeAdd.setOnClickListener {
            startActivity(Intent(this, OaAddMeActivity::class.java))
        }
    }

    /**
     * 获取个人信息
     */

    val array = ArrayList<OaShowMeEntity>()
    private suspend fun getPerson(){
        var seex: String? = null
        if (getToken().isNotEmpty()){
            api.loginAll(getToken()).apply {
                if (this.code == 200){
                    runOnUiThread {
                        this.user?.let {
                            if (it.sex == "1") seex = "女"
                            if (it.sex == "0") seex = "男"
                            array.add(OaShowMeEntity(it.userName.toString(), seex.toString(), it.phonenumber.toString()))


                            oaMeRv.apply {
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter = OaAddMeAdapter(array, R.layout.re_oa_show_me)
                                this.adapter?.notifyItemChanged(array.size)
                            }
                        }
                    }
                }else runOnUiThread { this.msg?.show() }
            }
        }else runOnUiThread { "自动生成个人信息失败！".show() }
    }


    override fun onStart() {
        super.onStart()
        if (OaName != null){
            array.add(OaShowMeEntity(OaName.toString(), OaSex.toString(), OaPhone.toString()))

            oaMeRv.apply {
                this.layoutManager = LinearLayoutManager(context)
                this.adapter = OaAddMeAdapter(array, R.layout.re_oa_show_me)
                this.adapter?.notifyItemChanged(array.size)
            }
        }
    }


    companion object{
        var OaName: String? = null
        var OaSex: String? = null
        var OaId: String? = null
        var OaTime: String? = null
        var OaPhone: String? = null
        var OaThis: String? = null
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}